//
// Created by Administrator on 24-8-22.
//

#include "ThreadMGR.h"

ThreadMGR::ThreadMGR() {

}

ThreadMGR::~ThreadMGR() {

}

void ThreadMGR::InitMutex() {
    // 创建互斥量
    osMutexDef(myMutex01);
    myMutex01Handle = osMutexCreate(osMutex(myMutex01));
}

void ThreadMGR::InitSemaphores() {
    // 创建二进制信号量
    osSemaphoreDef(myBinarySemLED);
    myBinarySemLEDHandle = osSemaphoreCreate(osSemaphore(myBinarySemLED), 1);

    // 创建计数信号量
    osSemaphoreDef(myCountingSem01);
    myCountingSem01Handle = osSemaphoreCreate(osSemaphore(myCountingSem01), 10);
}

void ThreadMGR::InitQueues() {
    // 创建消息队列
    osMessageQDef(myQueue01, 16, uint16_t);
    myQueue01Handle = osMessageCreate(osMessageQ(myQueue01), NULL);
}

void ThreadMGR::InitThreads() {
    // 创建任务1
//
//    osThreadDef(mytTask1, StartDefaultTask, osPriorityNormal, 0, 128);
//    mytTask1Handle = osThreadCreate(osThread(mytTask1), NULL);
//
//    // 创建任务2
//    osThreadDef(myTask02, StartTask02, osPriorityIdle, 0, 128);
//    myTask02Handle = osThreadCreate(osThread(myTask02), NULL);
//
//    // 创建任务3
//    osThreadDef(myTask03_key0, StartTask03, osPriorityIdle, 0, 128);
//    myTask03_key0Handle = osThreadCreate(osThread(myTask03_key0), NULL);
//
//    // 创建任务4
//    osThreadDef(myTask04_key1, StartTask04, osPriorityIdle, 0, 128);
//    myTask04_key1Handle = osThreadCreate(osThread(myTask04_key1), NULL);
}

void ThreadMGR::StartDefaultTask(void const * argument)
{
    /* USER CODE BEGIN StartDefaultTask */
    /* Infinite loop */
#ifdef TASK_CONTROL
    for(;;)
  {
      HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_RESET);
      osDelay(500);
      HAL_GPIO_WritePin(LED0_GPIO_Port,LED0_Pin,GPIO_PIN_SET);
      osDelay(500);
  }
#endif

#ifdef QUEUE_TEST
    for(;;)
  {
      osDelay(500);
  }
#endif

#ifdef  QUEUESAMPOR
    for(;;)
    {
        osDelay(10); // 延迟，防止任务占用过多CPU时间
    }
#endif

#ifdef BINARY_SEMAPHOR
    for(;;)
    {
        osDelay(10); // 延迟，防止任务占用过多CPU时间
    }
#endif

    /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void ThreadMGR::StartTask02(void const * argument)
{
    /* USER CODE BEGIN StartTask02 */
    /* Infinite loop */
#ifdef TASK_CONTROL
    BaseType_t xReturn = pdPASS;/* 定义�??????个创建信息返回�?�，默认为pdPASS */
  for(;;)
  {
      // 等待信号�??????
      HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET);
      osDelay(1000);
      HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET);
      osDelay(1000);

  }
#endif


#ifdef QUEUE_TEST
    uint8_t buttonPressed2 = 0; // 标志位，0表示按钮未按下，1表示按钮已按�????
    uint32_t data2;
    char buffer[50];
    /* Infinite loop */
    for(;;)
    {
        if(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == GPIO_PIN_RESET){
            osDelay(10); // 消抖
            if(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == GPIO_PIN_RESET)
            {
                //buttonPressed2 = 1; // 标志按钮已按�????
                BaseType_t status= xQueueReceive(myQueue01Handle,&data2,0);
                if(status!=pdPASS){
                    HAL_UART_Transmit(&huart1, (uint8_t *)"read queue error\n", sizeof("read queue error\n"), 200);
                }else{
                    // �?? data2 转换为字符串
                    printf("data:%ld",data2);
                }
            }

            while(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == GPIO_PIN_RESET);
        }


       // osDelay(1); // 延迟，防止任务占用过多CPU时间
    }
#endif

#ifdef  QUEUESAMPOR
    for(;;)
    {
        osDelay(10); // 延迟，防止任务占用过多CPU时间
    }
#endif


#ifdef BINARY_SEMAPHOR
    for(;;)
    {
        osDelay(10); // 延迟，防止任务占用过多CPU时间
    }
#endif
    /* USER CODE END StartTask02 */
}

/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the myTask03_key0 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void ThreadMGR::StartTask03(void const * argument)
{
    /* USER CODE BEGIN StartTask03 */
#ifdef TASK_CONTROL
    uint8_t buttonPressed = 0; // 标志位，0表示按钮未按下，1表示按钮已按�????
    /* Infinite loop */
    for(;;)
    {
        if(HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin) == GPIO_PIN_RESET)
        {
            osDelay(10); // 消抖
            if(HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin) == GPIO_PIN_RESET && buttonPressed == 0)
            {
                buttonPressed = 1; // 标志按钮已按�????

                if(mytTask1Handle == NULL)
                {
                    HAL_UART_Transmit(&huart1, (uint8_t *)"task1 not exist\n", sizeof("task1 not exist\n"), 200);
                    // 创建任务
                    osThreadDef(mytTask1, StartDefaultTask, osPriorityNormal, 0, 128);
                    mytTask1Handle = osThreadCreate(osThread(mytTask1), NULL);
                }
                else
                {
                    HAL_UART_Transmit(&huart1, (uint8_t *)"task1 deleted\n", sizeof("task1 deleted\n"), 200);
                    vTaskDelete(mytTask1Handle);
                    mytTask1Handle = NULL;
                }
            }
        }
        else
        {
            buttonPressed = 0; // 按钮松开时重置标志位
        }

        osDelay(100); // 延迟，防止任务占用过多CPU时间

    }
#endif

#ifdef QUEUE_TEST
    uint8_t buttonPressed = 0; // 标志位，0表示按钮未按下，1表示按钮已按�????
    uint32_t data=888;
    BaseType_t m_flag;
    /* Infinite loop */
    for(;;)
    {
        if(HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin) == GPIO_PIN_RESET){
            osDelay(10); // 消抖
            if(HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin) == GPIO_PIN_RESET && buttonPressed == 0)
            {
                buttonPressed = 1; // 标志按钮已按�????
                //写入data �??? queue 队列
                m_flag= xQueueSendToBack(myQueue01Handle,&data,200);
                if(pdPASS!=m_flag){
                    HAL_UART_Transmit(&huart1, (uint8_t *)"write 888 error\n", sizeof("write 888 error\n"), 200);
                }else{
                    HAL_UART_Transmit(&huart1, (uint8_t *)"write 888 to queue back\n", sizeof("write 888 to queue back\n"), 200);
                }
            }
        }else{
            buttonPressed = 0; // 按钮松开时重置标志位
        }
        osDelay(1); // 延迟，防止任务占用过多CPU时间
    }
#endif


#ifdef  QUEUESAMPOR
    uint8_t buttonPressed = 0; // 标志位，0表示按钮未按下，1表示按钮已按�????
    BaseType_t m_flag;
    /* Infinite loop */
    for(;;)
    {
        if(HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin) == GPIO_PIN_RESET){
            osDelay(10); // 消抖
            if(HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin) == GPIO_PIN_RESET && buttonPressed == 0)
            {
                buttonPressed = 1; // 标志按钮已按�????
                m_flag= xSemaphoreGive(myCountingSem01Handle);
                if(m_flag==pdTRUE){
                    HAL_UART_Transmit(&huart1, (uint8_t *)"park ok\n", sizeof("park ok\n"), 200);
                }else{
                    HAL_UART_Transmit(&huart1, (uint8_t *)"park err\n", sizeof("park err\n"), 200);
                }
            }
        }else{
            buttonPressed = 0; // 按钮松开时重置标志位
        }
        osDelay(1); // 延迟，防止任务占用过多CPU时间
    }
#endif

#ifdef BINARY_SEMAPHOR
    uint8_t buttonPressed = 0; // 标志位，0表示按钮未按下，1表示按钮已按�????
    BaseType_t m_flag;
    /* Infinite loop */
    for(;;)
    {
        if(HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin) == GPIO_PIN_RESET){
            osDelay(10); // 消抖
            if(HAL_GPIO_ReadPin(KEY0_GPIO_Port, KEY0_Pin) == GPIO_PIN_RESET && buttonPressed == 0)
            {
                buttonPressed = 1; // 标志按钮已按�????
                m_flag= xSemaphoreGive(myBinarySemLEDHandle);
                if(m_flag==pdTRUE){
                    HAL_UART_Transmit(&huart1, (uint8_t *)"GET DATA OK SET SEMPHO0R\n", sizeof("GET DATA OK SET SEMPHO0R\n"), 200);
                }else{
                    HAL_UART_Transmit(&huart1, (uint8_t *)"GET DATA OK UNSET SEMPHO0R\n", sizeof("GET DATA OK UNSET SEMPHO0R\n"), 200);
                }
            }
        }else{
            buttonPressed = 0; // 按钮松开时重置标志位
        }
        osDelay(1); // 延迟，防止任务占用过多CPU时间
    }
#endif
    /* USER CODE END StartTask03 */
}

/* USER CODE BEGIN Header_StartTask04 */
/**
* @brief Function implementing the myTask04_key1 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask04 */
void ThreadMGR::StartTask04(void const * argument)
{
    /* USER CODE BEGIN StartTask04 */
#ifdef TASK_CONTROL
    uint8_t buttonPressed = 0; // 标志位，0表示按钮未按下，1表示按钮已按�????

    /* Infinite loop */
    for(;;)
    {
        if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET)
        {
            osDelay(10); // 消抖
            if (HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET && buttonPressed == 0)
            {
                buttonPressed = 1; // 标志按钮已按�????

                if (flag == 1)
                {
                    HAL_UART_Transmit(&huart1, (uint8_t *)"task2 not suspend\n", sizeof("task2 not suspend\n"), 200);
                    vTaskSuspend(myTask02Handle);
                    flag = 0;
                }
                else
                {
                    HAL_UART_Transmit(&huart1, (uint8_t *)"task2 resume\n", sizeof("task2 resume\n"), 200);
                    vTaskResume(myTask02Handle);
                    flag = 1;
                }
            }
        }
        else
        {
            buttonPressed = 0; // 按钮松开时重置标志位
        }

        osDelay(100); // 延迟，防止任务占用过多CPU时间
    }
#endif

#ifdef QUEUE_TEST
    uint8_t buttonPressed1 = 0; // 标志位，0表示按钮未按下，1表示按钮已按�????
    uint32_t data1=999;
    BaseType_t m_flag1;
    /* Infinite loop */
    for(;;)
    {
        if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET){
            osDelay(10); // 消抖
            if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET && buttonPressed1 == 0)
            {
                buttonPressed1 = 1; // 标志按钮已按�????
                //写入data �??? queue 队列
                m_flag1= xQueueSendToBack(myQueue01Handle,&data1,200);
                if(pdPASS!=m_flag1){
                    HAL_UART_Transmit(&huart1, (uint8_t *)"write 999 error\n", sizeof("write 999 error\n"), 200);
                }else{
                    HAL_UART_Transmit(&huart1, (uint8_t *)"write 999 to queue back\n", sizeof("write 999 to queue back\n"), 200);
                }
            }
        }else{
            buttonPressed1 = 0; // 按钮松开时重置标志位
        }
        osDelay(1); // 延迟，防止任务占用过多CPU时间
    }
#endif

#ifdef  QUEUESAMPOR
    uint8_t buttonPressed = 0; // 标志位，0表示按钮未按下，1表示按钮已按�????
    BaseType_t m_flag;
    /* Infinite loop */
    for(;;)
    {
        if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET){
            osDelay(10); // 消抖
            if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET && buttonPressed == 0)
            {
                buttonPressed = 1; // 标志按钮已按�????
                m_flag= xSemaphoreTake(myCountingSem01Handle,0);
                if(m_flag==pdTRUE){
                    HAL_UART_Transmit(&huart1, (uint8_t *)"driver ok\n", sizeof("driver ok\n"), 200);
                }else{
                    HAL_UART_Transmit(&huart1, (uint8_t *)"on car\n", sizeof("on car\n"), 200);
                }
            }
        }else{
            buttonPressed = 0; // 按钮松开时重置标志位
        }
        osDelay(1); // 延迟，防止任务占用过多CPU时间
    }
#endif

#ifdef BINARY_SEMAPHOR
    uint8_t buttonPressed = 0; // 标志位，0表示按钮未按下，1表示按钮已按�????
    BaseType_t m_flag;
    /* Infinite loop */
    for(;;)
    {
        if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET){
            osDelay(10); // 消抖
            if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET && buttonPressed == 0)
            {
                buttonPressed = 1; // 标志按钮已按�????
                m_flag= xSemaphoreTake(myBinarySemLEDHandle,0);
                if(m_flag==pdTRUE){
                    HAL_UART_Transmit(&huart1, (uint8_t *)"update DATA OK SET SEMPHO0R\n", sizeof("update DATA OK SET SEMPHO0R\n"), 200);
                }else{
                    HAL_UART_Transmit(&huart1, (uint8_t *)"update DATA OK UNSET SEMPHO0R\n", sizeof("update DATA OK UNSET SEMPHO0R\n"), 200);
                }
            }
        }else{
            buttonPressed = 0; // 按钮松开时重置标志位
        }
        osDelay(1); // 延迟，防止任务占用过多CPU时间
    }
#endif
    /* USER CODE END StartTask04 */
}

void ThreadMGR::initThread() {
    InitMutex();
    InitSemaphores();
    InitQueues();
    InitThreads();
}
